package com.castile.parking.member.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger 配置类
 *
 * @description:
 * @author: Castile
 * @create: 2024-04-20 21:45
 * @Version 1.0
 **/
@EnableSwagger2
@Configuration
@Profile({"dev", "test"}) // 只在测试环境或者开发环境中启动swagger、在生产环境下这个类是不会加载的
public class Swagger2Config {

    @Bean
    public Docket createRestApi() {
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        ArrayList<Parameter> parameters = new ArrayList<>();
        parameterBuilder.modelRef(new ModelRef("String")).name("token").parameterType("header").description("token").defaultValue("").
                //考虑到有些请求是不需要 token 的，此处不能置必填
                        required(false).build();
        parameters.add(parameterBuilder.build());
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build().globalOperationParameters(parameters).securitySchemes(getSecuritySchemes());
    }

    private List<ApiKey> getSecuritySchemes() {
        List<ApiKey> keys = new ArrayList<>();
        ApiKey apiKey = new ApiKey("token", "token", "header");
        keys.add(apiKey);
        return keys;
    }

    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Member会员模块接口层(基于SpringBoot2+Swagger2).")
                .contact(new Contact("Castile", "https://castile.github.io", "hongliangzhu2019@gmail.com"))
                .version("1.0")
                .description("desc")
                .build();

    }

}
